home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / cimr.c < prev    next >
C/C++ Source or Header  |  1998-07-12  |  7KB  |  237 lines

  1. /* Citadel Internet Mail reader V1.00
  2. ** this routine will scan a directory specified in arg1
  3. ** Each entry is expected to be a mail message.
  4. ** The subject is tested against arg2(if found)
  5. **
  6. */
  7. #include <libraries/dos.h>
  8. #include <dos.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <proto/dos.h>
  13. #include "sysdep.h"
  14.  
  15. extern int _OSERR;
  16. char mode_flag;    /* U - uuencoded mail, B - Base 64 */
  17. char debug_flag;   /* V - verbose, D - full debug */
  18.  
  19. int Process_File(char *mail, char *subject, char *dump, char *processed);
  20. int main(int,char **);
  21. int Do_Break(void);
  22. char *Find_Text(FILE *fp, char *text);
  23.  
  24. int main( argc, argv)
  25. int argc;     /* should be 4 */
  26. char *argv[]; /* current location */
  27.   {
  28.   int status;
  29.   printf("Citadel Internet Mail Reader %s\n",VERSION_NAME);
  30.  
  31.   if( onbreak(&Do_Break) )printf("Cannot set break for ^C");
  32.  
  33.   if( argc != 5 && argc != 6 && argc != 7 )
  34.     {
  35.     printf("Please setup this program with the proper arguments.\n");
  36.     printf("CIMR arg1 arg2 arg3 arg4 [arg5]\n");
  37.     printf("   arg1: This is the mail full path name such as cit:mail/*.am\n");
  38.     printf("         which is where your mailer stores mail messages\n");
  39.     printf("   arg2: This is the subject header for the messages to file\n");
  40.     printf("         to search for.\n");
  41.     printf("   arg3: Where to dump the uuencoded message(full pathname)\n");
  42.     printf("   arg4: What do we do after processing\n");
  43.     printf("   arg5: Mode, U - uuencoded(default), or B - Base64\n");
  44.     printf("   arg6: Debug Mode - D, full debug or V, verbose mode\n");
  45.     printf(" This program will read the files specified in arg1, find the subject\n");
  46.     printf(" header specified in arg2 dumping the uuencoded portion(begin to end)\n");
  47.     printf(" into arg3, then save the used message (arg4=save) or remove it(arg4=remove\n");
  48.     printf("\n");
  49.     printf(" Status returned is 0 if message found\n");
  50.     printf("                    5 if message not found\n");
  51.     printf("                   20 if error occured\n");
  52.     status = 20;
  53.     }
  54.   else
  55.     {
  56.     debug_flag = ' ';
  57.     mode_flag  = 'U';  /* default is uuencoded */
  58.     if( argc >= 6 )
  59.       {
  60.       switch ( argv[5][0] )
  61.         {
  62.         case 'u': /* uuencoded */
  63.         case 'U': mode_flag = 'U'; break;
  64.         case 'b': /* base 64 */
  65.         case 'B': mode_flag = 'B'; break;
  66.         default: printf("Error: Invalid mode specifed:%s\n",argv[5]);
  67.         };
  68.       if( argc == 7 )
  69.         {
  70.         switch (argv[6][0])
  71.           {
  72.           case 'd':  /* full debug */
  73.           case 'D':  debug_flag = 'D'; break;
  74.           case 'v':  /* verbose mode */
  75.           case 'V':  debug_flag = 'V'; break;
  76.           default: printf("Error: invalid debug mode specified:%s\n",argv[6]);
  77.           };
  78.         };
  79.       };
  80.     if( debug_flag == 'D' )
  81.       {
  82.       printf("  mail path: %s\n",argv[1]);
  83.       printf("    subject: %s\n",argv[2]);
  84.       printf("destination: %s\n",argv[3]);
  85.       printf(" resolution: %s\n",argv[4]);
  86.       printf("       mode: %s\n",argv[5]);
  87.       };
  88.     status = Process_File(argv[1], argv[2], argv[3], argv[4]);
  89.     };
  90.   return status;
  91.   }
  92.  
  93. int Process_File(char *mail, char *subject, char *dump, char *processed)
  94.   {
  95.   FILE *fp;
  96.   FILE *op;
  97.   struct FileInfoBlock *info;
  98.   int error,attr;
  99.   char *ptr, *tptr;
  100.   char *dir;
  101.   char fullname[128];
  102.   if( debug_flag == 'V' || debug_flag == 'D')
  103.     {
  104.     printf("Process_File(%s, %s, %s, %s)\n", mail, subject, dump, processed);
  105.     };
  106.   dir = strdup(mail);
  107.   ptr = &dir[strlen(dir)-1];
  108.   while( *ptr != '/' && *ptr != ':' && ptr != dir)ptr--;
  109.   ptr[1] = '\0';
  110.   info  = (struct FileInfoBlock *)calloc(1,sizeof(struct FileInfoBlock));
  111.   if( info == NULL )
  112.     {
  113.     printf("Unable to get memory for FileInfoBlock, aborting\n");
  114.     return 20;
  115.     };
  116.  
  117.   attr = 1; /* find all files not directories */
  118.   error = dfind(info,mail,attr);  /* get first one*/
  119.   while( error == 0 )
  120.     {
  121.     /**
  122.       For each file, read the lines until a subject is found and
  123.       process the subject.
  124.     **/
  125.     if( debug_flag == 'V' || debug_flag == 'D')printf("processing %s\n",info->fib_FileName);
  126.     strcpy(fullname,dir);
  127.     strcat(fullname,info->fib_FileName);
  128.     if(  (fp = fopen(fullname,"r")) == NULL )
  129.       {
  130.       printf("unable to open %s, skipping it...\n", fullname);
  131.       }
  132.     else
  133.       {
  134.       if( ( ptr = Find_Text(fp,"subject")) != NULL )
  135.         {
  136.         tptr = &ptr[9];            /* start of subject */
  137.         if( debug_flag == 'D') printf("Found:%s\n",ptr);
  138.         if( strnicmp(tptr,subject,strlen(subject)) == 0)
  139.           {
  140.           char *string;
  141.           free(ptr);
  142.           string = (mode_flag == 'U') ? "begin 644" : "Content-Transfer-Encoding: base64";
  143.           if( ( ptr = Find_Text(fp,string) ) != NULL )
  144.             {
  145.             if( debug_flag == 'D') printf("Found:%s\n",ptr);
  146.             if( ( op=fopen(dump,"w")) == NULL )
  147.               {
  148.               printf("unable to open output file:%s\n",dump);
  149.               free(ptr);
  150.               return 20;
  151.               }
  152.             else
  153.               {
  154.               char line[80];
  155.               /* copy the file to the output */
  156.               if( mode_flag == 'B' )
  157.                 {  /* the base64 "header" stuff */
  158.                 while( fgets(line,sizeof(line), fp) )
  159.                   {
  160.                   if( strncmp(line,"Content-",8) == 0 )continue;
  161.                   if( line[0] == '\n' )continue;
  162.                   break;
  163.                   };
  164.                 fputs(line,op); /* write first line of base64 data */
  165.                 }
  166.               else
  167.                 {
  168.                 fputs(ptr,op);  /* write first line for uuencode data */
  169.                 };
  170.               free(ptr);
  171.               while( fgets(line,sizeof(line),fp) )fputs(line,op);
  172.               fclose(op);
  173.               fclose(fp);
  174.               /*
  175.                check processed for "save" or "remove"
  176.               */
  177.               if( stricmp(processed,"remove") == 0 )
  178.                 {
  179.                 printf("deleting %s\n",fullname);
  180.                 remove(fullname);
  181.                 }
  182.               else if( stricmp(processed,"save") != 0 )
  183.                 {
  184.                 printf(" Invalid option, should be save or remove was %s\n",processed);
  185.                 printf(" assumed save\n");
  186.                 };
  187.               return 0;   /* success! */
  188.               };
  189.             };
  190.           }
  191.         else
  192.           {
  193.           free(ptr);
  194.           fclose(fp);
  195.           };
  196.         }
  197.       else
  198.         {
  199.         free(ptr);
  200.         };
  201.       fclose(fp);
  202.       };
  203.     error = dnext(info);
  204.     };
  205.  
  206.   return 5;
  207.   }
  208.  
  209. char *Find_Text(FILE *fp, char *text)
  210.   {
  211.   char line[80];  /* assume lines are 80 char or less */
  212.   int  len;
  213.   /**
  214.     read the file processing each line until we find "text"
  215.     as the first thing on the line.
  216.   **/
  217.   if( debug_flag == 'D') printf("Searching for:%s\n",text);
  218.   len = strlen(text);
  219.   while( fgets(line,sizeof(line),fp) )
  220.     {
  221.     if( debug_flag == 'D') printf("%s is text found\n",line);
  222.     if( strnicmp(text,line,len) == 0 )
  223.       {
  224.       return strdup(line);
  225.       };
  226.     };
  227.   return NULL;
  228.   }
  229.  
  230. int Do_Break()
  231.   {
  232.   printf("\n ^C - ***Break***\n ");
  233.   (void)fcloseall();
  234.   return(1);
  235.  
  236.   }
  237.